import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Put,
  Headers,
  UseGuards,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { IpAddress } from 'src/decorators/ipAddress.decorator';
import { CurUser } from 'src/decorators/user.decorator';
import { AuthGuard } from '../auth/auth.guard';
import { UserEntity } from '../user/entities/user.entity';
import { ArticleService } from './article.service';
import { ArticleDto } from './dto/article.dto';
import { UpdateArticleDto } from './dto/update.article.dto';

@ApiTags('后台管理系统-文章管理')
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}
  /**
   * @Author: laotongshu
   * @Date: 2022-09-24 11:45:26
   * @LastEditors: laotongshu
   * @Description: 创建文章（增）
   * @param {Object} {}
   * @return {*}
   */
  @ApiOperation({ summary: '创建文章' })
  @UseGuards(AuthGuard)
  @Post()
  async createArticle(@Body() articleDto: ArticleDto, @Headers() header: any) {
    return await this.articleService.createArticle(articleDto, header);
  }

  /**
   * @Author: laotongshu
   * @Date: 2022-09-24 11:45:53
   * @LastEditors: laotongshu
   * @Description: 文章列表（查）
   * @param {Object} {}
   * @return {*}
   */
  @ApiOperation({ summary: '查询文章列表' })
  @Get()
  async getArticle() {
    return await this.articleService.getArticle();
  }

  /**
   * @Author: laotongshu
   * @Date: 2022-09-24 11:46:06
   * @LastEditors: laotongshu
   * @Description: 单个文章删除（删）
   * @param {Object} {}
   * @return {*}
   */
  @ApiOperation({ summary: '单个文章删除' })
  @Delete(':id')
  async delArticle(@Param('id') id: string) {
    return await this.articleService.delArticle(id);
  }

  /**
   * @Author: laotongshu
   * @Date: 2022-09-24 11:46:17
   * @LastEditors: laotongshu
   * @Description: 修改文章（改）
   * @param {Object} {}
   * @return {*}
   */
  @ApiOperation({ summary: '修改文章' })
  @Put()
  async updateArticle(@Body() updateArticleDto: UpdateArticleDto) {
    return await this.articleService.updateArticle(updateArticleDto);
  }
  /**
   * @Author: laotongshu
   * @Date: 2022-09-24 11:46:28
   * @LastEditors: laotongshu
   * @Description: 根据id查询文章详情
   * @param {Object} {}
   * @return {*}
   */
  @ApiOperation({ summary: '根据id查询文章详情' })
  @Get(':id')
  async articleById(@Param('id') id: string, @IpAddress() ipAddress: string) {
    return await this.articleService.articleById(id, ipAddress);
  }

  /**
   * @Author: laotongshu
   * @Date: 2022-10-09 10:28:42
   * @LastEditors: laotongshu
   * @Description: 根据文章id进行点赞
   * @param {Object} {}
   * @return {*}
   */
  @ApiOperation({ summary: '根据文章id进行点赞' })
  @Put('like/:id')
  async clickLike(@Param('id') id: string, @CurUser() user: UserEntity) {
    return this.articleService.clickLike(id, user.id);
  }
}
